/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.netbuilder.services.equipment;

import com.google.code.netbuilder.dal.entities.Equipment;
import com.google.code.netbuilder.dal.entities.EquipmentTemplate;
import com.google.code.netbuilder.dal.entities.Port;
import com.google.code.netbuilder.dal.entities.Wire;
import com.google.code.netbuilder.dal.providers.IEquipmentProvider;
import com.google.code.netbuilder.dal.providers.IEquipmentTemplateProvider;
import com.google.code.netbuilder.dal.providers.IProviderManager;
import com.google.code.netbuilder.dal.providers.IWireProvider;
import com.google.code.netbuilder.logging.NetBuilderLogger;
import com.google.code.netbuilder.logging.NetBuilderLoggerFactory;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.primefaces.model.map.Polyline;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.event.map.StateChangeEvent;
/**
 *
 * @author marksist
 */
@ManagedBean(name = "equipmentCreateMap")
@ViewScoped
public class EquipmentCreateMap {
    
    private static final NetBuilderLogger LOGGER = NetBuilderLoggerFactory.getNetBuilderLogger(EquipmentCreateMap.class);
    
    @EJB
    private IEquipmentProvider ep;
    
    @EJB
    private IWireProvider wp;
    
    @EJB
    private IProviderManager pm;

    @EJB
    private IEquipmentTemplateLoader eqTLoader;
    
    @EJB
    private IEquipmentTemplateProvider etp;
    
    private MapModel markerModel = new DefaultMapModel();
    
    private Equipment currentEquipment;
    private Equipment resultEquipment;
    private String templateName;
    
    private List<String> templateNames;
    
    private Marker selectMarker;
    private int zoom = 12;
    
    public EquipmentCreateMap(){
        currentEquipment = new Equipment();
        resultEquipment = new Equipment();
        currentEquipment.setLatityde(51.660387);
        currentEquipment.setLongitude(39.202051);
    }

    public int getZoom() {
        return zoom;
    }

    public void setZoom(int zoom) {
        this.zoom = zoom;
    }
    
    public Marker getSelectMarker() {
        return selectMarker;
    }

    public void setSelectMarker(Marker selectMarker) {
        this.selectMarker = selectMarker;
    }
    
    public Equipment getCurrentEquipment() {
        return currentEquipment;
    }

    public void setCurrentEquipment(Equipment currentEquipment) {
        this.currentEquipment = currentEquipment;
    }

    public Equipment getResultEquipment() {
        return resultEquipment;
    }

    public void setResultEquipment(Equipment resultEquipment) {
        this.resultEquipment = resultEquipment;
    }
    
    public List<String> getTemplateNames() {
        List<EquipmentTemplate> templates;
        templates = etp.findAll();
        templateNames = new ArrayList<String>();
        for(EquipmentTemplate et: templates){
            templateNames.add(et.getName());
        }
        return templateNames;
    }

    public void setTemplateNames(List<String> templateNames) {
        this.templateNames = templateNames;
    }

       
    public MapModel getMarkerModel(){

        List<Equipment> equipments = ep.findAll();
        List<Wire> wires = wp.findAll();
        
        Wire wire = null;
        Port port1 = null,
             port2 = null;
        for (int i=0; i< wires.size(); i++){
            wire = wires.get(i);
            port1 = wire.getFromPort();
            port2 = wire.getToPort();
            if (port1 == null || port2 == null) continue;
            Equipment eq1 = port1.getEquipment();
            Equipment eq2 = port2.getEquipment();
            
            Polyline polyline = new Polyline();
            polyline.getPaths().add(new LatLng(eq1.getLatityde(), eq1.getLongitude()));
            polyline.getPaths().add(new LatLng(eq2.getLatityde(), eq2.getLongitude()));
            polyline.setStrokeWeight(3);
            polyline.setStrokeColor("#B72626");
            polyline.setStrokeOpacity(0.5);
            markerModel.addOverlay(polyline);
        }
        
        for(Equipment eqipment : equipments) {
            LatLng coord = new LatLng(eqipment.getLatityde(), eqipment.getLongitude());
            Marker marker = new Marker(coord, eqipment.getName(), eqipment);
            markerModel.addOverlay(marker);
        }
        
        return markerModel;
    }
    
    public void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    } 
    
    public void createEquipment(ActionEvent actionEvent) {
        resultEquipment = eqTLoader.loadEquipmentFromTemplateName(templateName);
        
        resultEquipment.setAddress(currentEquipment.getAddress());
        resultEquipment.setLatityde(currentEquipment.getLatityde());
        resultEquipment.setLongitude(currentEquipment.getLongitude());
        resultEquipment.setName(currentEquipment.getName());
        
        try{
            ep.save(resultEquipment);
        } catch (Exception ex){
            LOGGER.debug(ex.getMessage());
            return;
        }
        
        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Create equipment", "Lat:" +
                currentEquipment.getLatityde() + ", Lng:" + currentEquipment.getLongitude()));
    }
    
    public void onMarkerSelect(OverlaySelectEvent event) {  
        selectMarker = (Marker) event.getOverlay();
    }
    
    public void onStateChange(StateChangeEvent event) {  
        zoom = event.getZoomLevel();   
    }
    
    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }
    
}
